package kr.co.casually.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class KeyWordsDbAdapter {

	public static final String KEY_GENRE_CODE = "genrecode"; // 장르 코드
	public static final String KEY_GENRE_NAME = "genrename"; // 장르 명
	public static final String KEY_STARTDATE = "satrtdate"; // 시작일
	public static final String KEY_ENDDATE = "enddate"; // 종료일
	public static final String KEY_TITLE = "title"; // 명칭

	public static final String KEY_ROWID = "_id"; // idx

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "seoulconcert_key.db";
	private static final String DATABASE_TABLE = "keywords";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (" + KEY_ROWID
			+ " integer primary key autoincrement, " + KEY_GENRE_CODE
			+ " INTEGER, " + KEY_GENRE_NAME + " TEXT, " + KEY_STARTDATE
			+ " TEXT, " + KEY_ENDDATE + " TEXT, " + KEY_TITLE + " TEXT);";

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			/*Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");*/
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
	}

	public KeyWordsDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public KeyWordsDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	// 키워드 등록
	public long createKeyWord(int genreCode, String genreName,
			String startDate, String endDate, String title) {
		ContentValues initialValues = new ContentValues();

		initialValues.put(KEY_GENRE_CODE, genreCode);
		initialValues.put(KEY_GENRE_NAME, genreName);
		initialValues.put(KEY_STARTDATE, startDate);
		initialValues.put(KEY_ENDDATE, endDate);
		initialValues.put(KEY_TITLE, title);

		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	// 키워드 삭제
	public boolean deleteKeyWord(long rowId) {

		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	// 키워드 모두 가져오기
	public Cursor fetchAllKeyWords() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_GENRE_CODE, KEY_GENRE_NAME, KEY_STARTDATE, KEY_ENDDATE,
				KEY_TITLE }, null, null, null, null, null);
	}

	// 키워드 1줄 가져오기
	public Cursor fetchKeyWord(long rowId) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_GENRE_CODE, KEY_GENRE_NAME, KEY_STARTDATE, KEY_ENDDATE,
				KEY_TITLE }, KEY_ROWID + "=" + rowId, null, null, null, null,
				null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	// 키워드 수정
	public boolean updateKeyWord(long rowId, int genreCode, String genreName,
			String startDate, String endDate, String title) {
		ContentValues args = new ContentValues();
		args.put(KEY_GENRE_CODE, genreCode);
		args.put(KEY_GENRE_NAME, genreName);
		args.put(KEY_STARTDATE, startDate);
		args.put(KEY_ENDDATE, endDate);
		args.put(KEY_TITLE, title);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
	
	//장르코드 group
	public Cursor fetchCode(){
		Cursor c = mDb.rawQuery("SELECT genrecode FROM keywords GROUP BY genrecode" , null);
		return c;
	}

}
